package make.free.app.mcaro.activity;

import java.util.HashMap;
import java.util.Map;

import make.free.app.mcaro.R;
import make.free.app.mcaro.facebook.FacebookEventObserver;
import make.free.app.mcaro.util.Constan;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.nostra13.socialsharing.common.AuthListener;
import com.nostra13.socialsharing.facebook.FacebookFacade;


public class FacebookActivity extends Activity {

	private FacebookFacade facebook;
	private FacebookEventObserver facebookEventObserver;
//	private SharedPreferencesDB db;

	private TextView messageView;

	private String link = Constan.URLLAPP;
	private String linkName =Constan.NAMEAPP;
	private String linkDescription = "Game Caro bluetooth";
	private String picture="http://i121.photobucket.com/albums/o233/nam_phong3/button_sendcopy_zpsb511d4c8.png";
	
	
	private Map<String, String> actionsMap;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.ac_facebook);
		Data();
//		db = new SharedPreferencesDB(this);
		facebook = new FacebookFacade(this, Constan.FACEBOOK_APP_ID);
		facebookEventObserver = FacebookEventObserver.newInstance();

		messageView = (TextView) findViewById(R.id.message);
		TextView linkNameView = (TextView) findViewById(R.id.link_name);
		TextView linkDescriptionView = (TextView) findViewById(R.id.link_description);
		Button postButton = (Button) findViewById(R.id.button_post);
	
		actionsMap = new HashMap<String, String>();
		actionsMap.put(Constan.FACEBOOK_SHARE_ACTION_NAME,
				Constan.FACEBOOK_SHARE_ACTION_LINK);

		messageView.setText("type input text");
		linkNameView.setText(linkName);
		linkDescriptionView.setText(linkDescription);
		
		postButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (facebook.isAuthorized()) {
//					db.setMask(String.valueOf(Integer.parseInt(db.getMask())+1));
//					Log.e("DATAbase", db.getMask());
					publishMessage();
					finish();
				} else {
					// Start authentication dialog and publish message after
					// successful authentication
					facebook.authorize(new AuthListener() {
						@Override
						public void onAuthSucceed() {
							publishMessage();
							finish();
						}

						@Override
						public void onAuthFail(String error) { // Do noting
						}
					});
				}
			}
		});
	}

	private void publishMessage() {
		facebook.publishMessage(messageView.getText().toString(), link,
				linkName, linkDescription, picture, actionsMap);
	}

	@Override
	public void onStart() {
		super.onStart();
		facebookEventObserver.registerListeners(this);
		if (!facebook.isAuthorized()) {
			facebook.authorize();
		}
	}

	@Override
	public void onStop() {
		facebookEventObserver.unregisterListeners();
		super.onStop();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_facebook_twitter, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.item_logout:
			facebook.logout();
			return true;
		default:
			return false;
		}
	}
	public void Data()
	{
		try {
			Bundle data = getIntent().getExtras();
			String link_web = data.getString("link");
			linkName = data.getString("title")+"\n"+link_web;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(!haveNetworkConnection())
		{
			showDialogError();
		}
	}
	private boolean haveNetworkConnection() {
		boolean haveConnectedWifi = false;
		boolean haveConnectedMobile = false;

		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo[] netInfo = cm.getAllNetworkInfo();
		for (NetworkInfo ni : netInfo) {
			if (ni.getTypeName().equalsIgnoreCase("WIFI"))
				if (ni.isConnected())
					haveConnectedWifi = true;
			if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
				if (ni.isConnected())
					haveConnectedMobile = true;
		}
		return haveConnectedWifi || haveConnectedMobile;
	}

	private void showDialogError() {
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

		// set title
		alertDialogBuilder.setTitle("Lỗi mạng!");

		// set dialog message
		alertDialogBuilder.setMessage("Bạn chưa kết nối WIFI hoặc 3G")
				.setCancelable(false)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						// if this button is clicked, close
						// current activity
						finish();
						dialog.dismiss();
					}
				});

		// create alert dialog
		AlertDialog alertDialog = alertDialogBuilder.create();

		// show it
		alertDialog.show();

	}
}
